import { game, native } from "cc";
import { AndroidInterface } from "myGlobalModule";
import { App } from "../../../src/app";
import { ShareInfo } from "../const/enumConst";
import { EventName } from "../const/eventName";
import { E_operationType, NativePlatform, Native_Operation } from "./nativePlatform";

export class AndroidPlatform extends NativePlatform {

	private callback: Function = null;

	/**
	 * 安卓app调用游戏端方法 需要将方法注入到window对象中
	 */
	injectGlobalFun() {
		if (!window.android) {
			window.android = {} as AndroidInterface
		}
		window.android.refreshBalance = () => {

		}
	}

	getUUid(): string {
		return this.sendNativeMsg(Native_Operation.GetUUid);
	}

	changeOrientation(isPortrait: boolean) {
		if (isPortrait == this.isPortrait) {
			return;
		}
		this.isPortrait = isPortrait;

		let direStr = isPortrait ? "V" : "H";
		this.sendNativeMsg(Native_Operation.ChangeOrientation, direStr);
		super.changeOrientation(isPortrait);
	}

	savePictureToAlbum(data: string) {
		this.sendNativeMsg(Native_Operation.SavePicture, data);
	}

	copyToClipboard(content: string): void {
		this.sendNativeMsg(Native_Operation.CopyToClipboard, content);
	}

	getClipboardContent(callback?: Function): Promise<string> {
		this.callback = callback;
		return this.sendNativeMsg(Native_Operation.GetClipboardContent);
	}

	getPackageName(): Promise<string> {
		this.sendNativeMsg(Native_Operation.GetPackageName);
		return new Promise((r) => {
			// App.event.on(EventName.Lobby.GETPACKAGENAME, r, this)
		})

	}
	getAppVersion(): string {
		return this.sendNativeMsg(Native_Operation.GetAppVersion);
	}
	checkIsEmulator(): boolean {
		let str = this.sendNativeMsg(Native_Operation.CheckIsEmulator);
		return !!str;
	}

	faceBookLogin(): string {
		return this.sendNativeMsg(Native_Operation.FaceBookLogin);
	}

	faceBookLoginOut(): boolean {
		return this.sendNativeMsg(Native_Operation.FaceBookOut);
	}

	loginGoogle() {
		return this.sendNativeMsg(Native_Operation.LoginGoogle);
	}

	loginOutGoogle() {
		return this.sendNativeMsg(Native_Operation.LoginOutGoogle);
	}

	faceBookShare(content: ShareInfo) {
		return this.sendNativeMsg(Native_Operation.FaceBookShare, JSON.stringify(content));
	}

	whatsAppShare(url: string) {
		return this.sendNativeMsg(Native_Operation.WhatsAppShare);
	}

	sendNativeMsg(operation: string, args: string = '') {
		// return jsb.reflection.callStaticMethod("org/cocos2dx/javascript/AppActivity", "revJsMessage",
		//     "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;", operation, args);       
		native.bridge.sendToNative(operation, args);
		return null;
	}

	receiveNativeMsg(operation: string, data: string) {
		switch (operation) {
			case Native_Operation.SavePicture:
			case Native_Operation.CopyToClipboard:
				// App.view.showMsgTips(data);
				break;

			case Native_Operation.FaceBookLogin:
				// App.event.emit(EventName.Login.LOGIN_THIRD_WAY, data);
				console.log(data);
				break;

			case Native_Operation.FaceBookOut:
				console.log(data);
				break;

			case Native_Operation.LoginGoogle:
				// App.event.emit(EventName.Login.LOGIN_THIRD_WAY, data);
				break;

			case Native_Operation.LoginOutGoogle:
				console.log(data);
				break;

			case Native_Operation.GetClipboardContent:
				this.callback && this.callback(data);
				break;

			case Native_Operation.GetPackageName://android包名
				console.log(data);
				// App.event.emit(EventName.Lobby.GETPACKAGENAME, data);
				break;

			case Native_Operation.GetAppVersion://android APP 版本号
				console.log(data);
				// App.event.emit(EventName.Lobby.GETAPPVERSION, data);
				break;

			case Native_Operation.CheckIsEmulator://是否是模拟器
				if (!+data) {//禁止在模拟器上运行
					game.end();
				}
				break;

			case Native_Operation.UserInfo:
				App.event.emit(EventName.Game.USER_INFO, data);
				break;

			case Native_Operation.LeaveRoom:
				App.event.emit(EventName.Game.NATIVE_LEAVE_ROOM, data);
				break;

			case Native_Operation.PlayerTalk:
				App.event.emit(EventName.Game.NATIVE_PLAYER_TALK, data);
				break;

			default:
				break;
		}


	};

	/**
	 * 游戏调用原生的方法
	 * 1.获取用户信息 2.离开房间
	 */

	jsCallJava(operationType: number) {
		let eventName = ""
		let eventData = {}
		switch (operationType) {
			case E_operationType.UserInfo:
				eventName = E_operationType[E_operationType.UserInfo]
				break;
			case E_operationType.upMic:
				eventName = E_operationType[E_operationType.upMic]
				break;
			default:
				break;
		}


		let callObj = {
			event: eventName,
			data: eventData
		}
		native.reflection.callStaticMethod("com/youyue/chatbar/help/room/GameRoomHelper", "jsCallJava", "(Ljava/lang/String;)Ljava/lang/String;", JSON.stringify(callObj));
		// LogMgr.info(result); // 10
	}

	jsCallIos(): void {

	}



}


